import pymysql
import random

mysql_conf = {
    "host": "localhost",
    "user": "root",
    "password": "qwe369",
    "db": "tunan_class"
}

con_local = pymysql.connect(**mysql_conf)

# 实际操作都需要光标进行
cursor_local = con_local.cursor()


def insert_one():
    for i in range(20):
        SQL = f"""insert into qianjin(name) values ('it"s ok')"""
        print(SQL)
        # 光标执行我们的语句
        cursor_local.execute(SQL)
        # 保证上述提交的语句被执行
        con_local.commit()


def inert_many():
    SQL = """insert into qianjin(name) values (%s)"""
    cursor_local.executemany(SQL, [(f"name_{i}",) for i in range(15, 30)])
    con_local.commit()


def get_students():
    SQL = """select name from qianjin"""
    cursor_local.execute(SQL)
    # 接受Mysql返回的查询结果
    query_set = cursor_local.fetchall()
    print(type(query_set))
    for item in query_set:
        print(item)


if __name__ == "__main__":
    get_students()